package label.dao.impl;

import label.entity.LabelVO;
import generic.dao.impl.GenericDaoImpl;
import generic.page.Pages;
import label.dao.LabelDao;

import java.util.List;

public class LabelDaoImpl extends GenericDaoImpl<LabelVO,Integer> implements LabelDao {

    @Override
    public LabelVO existsOdLabel(LabelVO labelVO,Integer blogDataId) {
        Object[] params={labelVO.getLabelName(),blogDataId};
        List list=this.findObj("from LabelVO l where l.labelName=? and l.blogDataVO.blogDataId=?", params);
        if(list.size() == 0){
            return null;
        }else {
            return (LabelVO) list.get(0);
        }
    }

    @Override
    public LabelVO findLabelVOById(Integer labelId) {
        return (LabelVO)this.findById(LabelVO.class,labelId);
    }

    @Override
    public void deleteLabelById(Integer labelId) {
        this.delete(this.findLabelVOById(labelId));
    }

    @Override
    public LabelVO updateLabel(LabelVO labelVO) {
        this.update(labelVO);
        return labelVO;
    }

    @Override
    public LabelVO addLabelVO(LabelVO labelVO) {
        this.save(labelVO);
        return labelVO;
    }



    @Override
    public Pages queryPageByBlogData(Pages pages,Integer blogDataId) {

        StringBuffer hql2= new StringBuffer();
        hql2.append("select count(*) from LabelVO a where a.blogDataVO.id=").append(blogDataId);
        pages.setRowCount(Integer.parseInt(this.getSession().createQuery(hql2.toString()).uniqueResult().toString()));

        Pages.init(pages);

        //查询记录
        hql2.delete(0,15);
        List list = this.getSession().createQuery(hql2.toString())
                .setFirstResult(pages.getOffset())
                .setMaxResults(pages.getPageSize()).list();
        pages.setList(list);
        return pages;
    }
    public List<LabelVO> findAllLabelByBlogData(Integer blogDataId){
        Object[] params={blogDataId};
        List<LabelVO> list=this.findObj("from LabelVO l where l.blogDataVO.blogDataId=?", params);
        return list;
    }

}
